package com.emmy.walnut.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeStampUtil {
    private static Calendar calendar = Calendar.getInstance();
    private static Calendar calendar_2 = Calendar.getInstance();

    private static SimpleDateFormat dayFormat = new SimpleDateFormat("MM月dd日");
    private static SimpleDateFormat yearMonthDayFormat = new SimpleDateFormat("YYYYMMdd");

    /**
     * @param currentTime 当前时间
     * @return  当前时间所在天数的初始时间戳
     */
    public static long getDailyStartTime(long currentTime){
        calendar.setTimeInMillis(currentTime);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTimeInMillis();
    }

    /**
     * @param currentTime 当前时间
     * @return  当前时间所在天数的末尾时间戳
     */
    public static long getDailyEndTime(long currentTime){
        calendar.setTimeInMillis(currentTime);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * @param currentTime 当前时间
     * @return  当前时间所在星期的初始时间戳
     */
    public static long getWeekStartTime(long currentTime){
        calendar.setTimeInMillis(currentTime);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayofweek);
        return calendar.getTimeInMillis();
    }

    /**
     * @param currentTime 当前时间
     * @return  当前时间所在星期末尾时间戳
     */
    public static long getWeekEndTime(long currentTime){
        calendar.setTimeInMillis(getWeekStartTime(currentTime));
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * @param timeStamp 当前时间戳
     * @return 当前时间所在月份的初始时间戳
     */
    public static long getMonthStartTime(long timeStamp) {
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * @param timeStamp 当前时间戳
     * @return   当前时间所在月份的末尾时间戳
     */
    public static long getMonthEndTime(long timeStamp) {
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * @param timeStamp 当前时间戳
     * @return  当前时间所在年的初始时间戳
     */
    public static long getYearStartTime(long timeStamp){
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * @param timeStamp 当前时间戳
     * @return  当前时间所在年的末尾时间戳
     */
    public static long getYearEndTime(long timeStamp){
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * @param timeStamp 当前时间戳
     * @return  当月天数
     */
    public static int getDaysOfMonth(long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param timeStamp 当前时间戳
     * @return  当月总时间戳
     */
    public static long getMonthTimestamp(long timeStamp) {
        return getDaysOfMonth(timeStamp) * 1000* 60 * 60 * 24;
    }

    /**
     * @param timeStamp1
     * @param timeStamp2
     * @return
     */
    public static boolean isSameDate(long timeStamp1, long timeStamp2) {
        calendar.setTimeInMillis(timeStamp1);
        calendar_2.setTimeInMillis(timeStamp2);
        return calendar.get(Calendar.YEAR)==calendar_2.get(Calendar.YEAR)
                && calendar.get(Calendar.MONTH)==calendar_2.get(Calendar.MONTH)
                &&calendar.get(Calendar.DAY_OF_MONTH)==calendar_2.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param timeStamp 时间戳
     * @return  返回时间戳MM-DD的格式
     */
    public static String getDayFormat(long timeStamp) {
        return dayFormat.format(new Date(timeStamp));
    }

    /**
     * @param timeStamp 时间戳
     * @return  返回时间戳YYYY/MM/DD的格式
     */
    public static String getYearMonthDayFormat(long timeStamp) {
        return yearMonthDayFormat.format(new Date(timeStamp));
    }

    /**
     * @param mills 时间（毫秒数）
     * @return 返回格式化HH:MM:SS的字符串
     */
    public static String getFormatTime(int mills){
        StringBuilder stringBuilder = new StringBuilder();
        mills = (mills/1000);
        int second = mills % 60;
        mills = mills / 60;
        int minute = mills % 60;
        int hours = mills / 60;
        stringBuilder.append(hours<10?"0"+hours: hours);
        stringBuilder.append(":");
        stringBuilder.append(minute<10?"0"+minute: minute);
        stringBuilder.append(":");
        stringBuilder.append(second<10?"0"+second: second);
        return stringBuilder.toString();
    }

}
